<?php
/**
 * Group profile summary
 *
 * Icon and profile fields
 *
 * @uses $vars['group']
 */

if (!isset($vars['entity']) || !$vars['entity']) {
    echo elgg_echo('groups:notfound');
    return true;
}

$group = $vars['entity'];
$owner = $group->getOwnerEntity();

?>
<div class="groups-profile clearfix elgg-image-block">
    <div class="elgg-image">
        <div class="groups-profile-icon">
            <?php echo elgg_view_entity_icon($group, 'large', array('href' => '')); ?>
        </div>
        <div class="groups-stats">
            <p>
                <b><?php echo elgg_echo("groups:owner"); ?>: </b>
                <?php
                echo elgg_view('output/url', array(
                    'text' => $owner->name,
                    'value' => $owner->getURL(),
                    'is_trusted' => true,
                ));
                ?>
            </p>

            <p>
                <?php
                echo elgg_echo('groups:members') . ": " . $group->getMembers(0, 0, TRUE);
                ?>
            </p>
        </div>
    </div>

    <div class="groups-profile-fields elgg-body">
        <?php
        echo elgg_view('groups/profile/field', $vars);

        // TODO: Kha. Add SVN URL Repo (if granted)
        echo '<br/>';
        // linux shell command to execute
        $exec = 'find /opt/lampp/htdocs/igs/svnrepositories/ -type d -name ' . $group->getGUID();

        // execute the cmd and get output
        $output = shell_exec($exec);
        //system_message($output);
        // process the output
        // empty => not found => request to the SVN operator (inbox message) => system message
        // not empty => found => already request and proceeded => system message.

        if ($output) {
            echo '<b>' . elgg_echo('groups:svnurl') . ':  </b>' .
                elgg_view('output/url', array(
                    'text' => elgg_echo('groups:svnurlbase') . $group->getGUID(),
                    'value' => elgg_echo('groups:svnurlbase') . $group->getGUID(),
                    'is_trusted' => true,
                ));
        } else {
            echo '<b>' . elgg_echo('groups:svnurl') . ': </b>' . elgg_echo('groups:svnnotfound');
        }

        $user = elgg_get_logged_in_user_entity();

        if($group->grouptype != SUBJECT_GROUP){
        	echo '<br/><hr/>';

			$members = elgg_get_entities_from_relationship(array(
					'relationship' => 'member',
					'relationship_guid' => $group->getGUID(),
					'inverse_relationship' => true,
					'types' => 'user',
					'limit' => 20
			));
			
				
			echo '<b>Số lần commit mã nguồn SVN </b> <br>';
			foreach ($members as $member){
				if($member->usertype != "Teacher"){
					$report_svn = "<div style='margin-left:20px; margin-top:5px;'>";
					$report_svn .= elgg_view("output/url", array(
                        "text" => $member->name,
							"href" => "profile/" . $member->username,
					)) . ": ";
					//        var_dump($user->svn_commit_count);
					//        $user->svn_commit_count = null;
					if (!$member->svn_commit_count) {
						$report_svn .= "\t\t0"."<br>";
					} else {
						$count = 0;
						try {
							$array = json_decode($member->svn_commit_count, true);
							if (!empty($array[$group->guid])) {
                                $count = $array[$group->guid];
							}
						} catch (Exception $e) {
                            $report_svn .= "\t\t0"."<br>";
						}
			
						$report_svn .= $count."<br>";
					}
					$report_svn .= "</div>";
			
					echo $report_svn;
				}
			}
        }
        

        ?>
    </div>
</div>
<?php
?>

